package com.jinlia.show.modules.wxapp.service;

import com.jinlia.show.common.core.utils.LBSUtils;
import com.jinlia.show.modules.base.model.query.CompanyQuery;
import com.jinlia.show.modules.base.model.vo.CompanyVO;
import com.jinlia.show.modules.base.system.entity.Company;
import com.jinlia.show.modules.base.system.service.CompanyMapperService;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CompanyService extends CompanyMapperService {
    public List<CompanyVO> getNearByLocation(CompanyQuery companyQuery) {
        List<Company> list = this.list();
        List<CompanyVO> listVO = list.stream().map(CompanyVO::fromEntity).collect(Collectors.toList());
        if (companyQuery.getLatitude() != null) {
            for (CompanyVO companyVO : listVO) {
                double v = LBSUtils.distanceOfTwoPoints(Double.valueOf(companyQuery.getLongitude()), Double.valueOf(companyQuery.getLatitude()), companyVO.getLongitude(), companyVO.getLatitude());
                companyVO.setDistance(v / 1000); //距离千米
            }
            listVO.sort(Comparator.comparing(CompanyVO::getDistance));
        }
        return listVO;
    }
}
